package com.franklin.ideaplugin.easytesting.ui;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.franklin.ideaplugin.api.ui.ToolBarWindowForm;
import com.franklin.ideaplugin.api.utils.ExecuteToolBarUtils;
import com.franklin.ideaplugin.easytesting.action.ExecuteMethodAction;
import com.franklin.ideaplugin.easytesting.cache.IEasyTestingCache;
import com.franklin.ideaplugin.easytesting.cache.impl.AbstractEasyTestingCache;
import com.franklin.ideaplugin.easytesting.cache.impl.PersistenceEasyTestingCache;
import com.franklin.ideaplugin.easytesting.constant.ExecuteFormType;
import com.franklin.ideaplugin.easytesting.entity.ExecuteResult;
import com.franklin.ideaplugin.easytesting.filetype.EasyTestingFormFileType;
import com.franklin.ideaplugin.easytesting.icon.PluginIcons;
import com.franklin.ideaplugin.easytesting.ui.history.ExecuteHistoryFormNew;
import com.franklin.ideaplugin.easytesting.vfs.ExecuteMethodVirtualFile;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.*;
import com.intellij.openapi.fileEditor.impl.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.*;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.*;
import com.intellij.ui.popup.AbstractPopup;
import com.intellij.util.LocalTimeCounter;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

/**
 * @author Ye Junhui
 * @since 2023/6/21
 */
public class ExecuteMethodResultFormProvider {

    private static final Logger log = Logger.getInstance(ExecuteMethodAction.class);

    public void showExecuteMethodResultForm(PsiMethod psiMethod){

        Project project = psiMethod.getProject();
        FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);

        final PsiFileFactory factory = PsiFileFactory.getInstance(project);
        final long stamp = LocalTimeCounter.currentTime();
        EasyTestingFormFileType fileType = EasyTestingFormFileType.INSTANCE;
        final PsiFile psiFile = factory.createFileFromText(psiMethod.getName() + "." + fileType.getDefaultExtension(), fileType, "", stamp, true, false);

        FileEditorManagerImpl editorManager = (FileEditorManagerImpl) fileEditorManager;

        IEasyTestingCache easyTestingCache = IEasyTestingCache.getInstance(project);
        List<ExecuteResult> executeResultList = easyTestingCache.getExecuteResultList(psiMethod);
        ExecuteResult executeResult = null;
        if (CollectionUtil.isNotEmpty(executeResultList)){
            executeResult = executeResultList.get(0);
        }
        PersistenceEasyTestingCache persistenceEasyTestingCache = (PersistenceEasyTestingCache) easyTestingCache;
        String formPath = persistenceEasyTestingCache.getFormPath(psiMethod);
        File formFile = FileUtil.writeString("", formPath, StandardCharsets.UTF_8);
//        VirtualFile formVirtualFile = VirtualFileManager.getInstance().refreshAndFindFileByNioPath(formFile.toPath());
//        PsiManager psiManager = PsiManager.getInstance(project);
//        PsiFile formPsiFile = psiManager.findFile(formVirtualFile);
//        ExecuteMethodVirtualFile virtualFile = new ExecuteMethodVirtualFile(psiMethod,psiFile.getVirtualFile(),executeResult,fileType);
//        Document document = PsiDocumentManager.getInstance(project).getDocument(formPsiFile);
//        virtualFile.putUserData(FileDocumentManagerBase.HARD_REF_TO_DOCUMENT_KEY,document);
////        EditorWindow editorWindow = splitters.openInRightSplit(virtualFile);
//        editorManager.openFileInNewWindow(virtualFile);
//        if (Objects.isNull(editorWindow)){
//        }
        //弹窗保底
        ExecuteHistoryFormNew executeMethodResultForm = new ExecuteHistoryFormNew(psiMethod, ExecuteFormType.NORMAL);
        ToolBarWindowForm toolBarWindowForm = new ToolBarWindowForm();
        toolBarWindowForm.addContent(executeMethodResultForm.getMainPanel(),new Dimension(1300,800));
        toolBarWindowForm.addToolButton(executeMethodResultForm.getExecuteBtn());
        toolBarWindowForm.addToolButton(executeMethodResultForm.getRollbackExecuteBtn());
        toolBarWindowForm.addToolButton(executeMethodResultForm.getDownloadBtn());
        toolBarWindowForm.addToolButton(executeMethodResultForm.getThreadPoolExecuteBtn());
        ExecuteToolBarUtils.initToolBar(toolBarWindowForm);
        executeMethodResultForm.init();
        this.shopPopup(psiMethod,executeMethodResultForm,toolBarWindowForm);
        return;

    }

    private void shopPopup(PsiMethod psiMethod, ExecuteHistoryFormNew executeMethodResultForm, ToolBarWindowForm toolBarWindowForm){
//        JBPopupFactory jbPopupFactory = JBPopupFactory.getInstance();
//        ComponentPopupBuilder componentPopupBuilder = jbPopupFactory.createComponentPopupBuilder(toolBarWindowForm.getMainPanel(), toolBarWindowForm.getMainPanel());
//        JBPopup runMethodPopup = componentPopupBuilder
//                .setTitle("Execute Method")
//                .setCancelKeyEnabled(true)
//                .setCancelButton(new IconButton("close", AllIcons.Actions.CloseHovered))
//                .setTitleIcon(new ActiveIcon(PluginIcons.PLUGIN_ICON))
//                .setMovable(true)
//                .setShowShadow(true)
//                .setResizable(true)
//                .setRequestFocus(true)
//                .setCancelOnClickOutside(false)
//                .setCancelOnMouseOutCallback(mouseEvent -> false)
//                .setCancelOnOtherWindowOpen(false)
//                .setCancelOnWindowDeactivation(false)
//                .createPopup();
//
//        AbstractPopup abstractPopup = (AbstractPopup) runMethodPopup;
//        abstractPopup.addResizeListener(() -> {
//            executeMethodResultForm.resize(abstractPopup.getSize());
//        },runMethodPopup);
//
//        JComponent content = runMethodPopup.getContent();
//        executeMethodResultForm.setParent(content);
//        runMethodPopup.setSize(Sizes.ExecuteMethodFormSize.MAIN_SIZE);
//        runMethodPopup.showCenteredInCurrentWindow(psiMethod.getProject());
        TestFrame testFrame = new TestFrame(psiMethod,toolBarWindowForm);
        testFrame.show();
    }
}
